package com.gskj.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gskj.eduservice.client.VodClient;
import com.gskj.eduservice.entity.EduVideo;
import com.gskj.eduservice.mapper.EduVideoMapper;
import com.gskj.eduservice.service.IEduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-16
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements IEduVideoService {

    //注入远程接口
    @Autowired
    private VodClient vodClient;

    // 根据课程id 删除小节、视频
    @Override
    public void deleteVideoByCourseId(String remCourseId) {

        // 根据课程id 查询所有的小结信息
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id", remCourseId);
        // 设置要查询的字段
        eduVideoQueryWrapper.select("video_source_id");

        // 所有 小结信息
        //  // 根据小结集合 获取所有的视频id
        List<EduVideo> eduVideoList = baseMapper.selectList(eduVideoQueryWrapper);

        // 遍历 eduVideoList 将所有 视频id存放到 list中
        List<String> list = new ArrayList<>();

        for (EduVideo eduVideo : eduVideoList) {
            // 非空
            if (null != eduVideo && !StringUtils.isEmpty(eduVideo.getVideoSourceId())) {

                list.add(eduVideo.getVideoSourceId());
            }
        }
        // 非空
        if (list != null && list.size() > 0) {
            //  调用 远程方法 批量删除 视频信息
            vodClient.removeBatchVideoByIds(list);
        }

        // 根据课程id 删除 小结信息
       try{
           QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
           eduVideoQueryWrapper.eq("course_id", remCourseId);
           baseMapper.delete(wrapper);
       }catch ( Exception e){
           e.printStackTrace();
       }
    }
}
